//
//  CLLWelcomeView.swift
//  CLWeiNo
//
//  Created by DDC on 2017/10/13.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit

class CLLWelcomeView: UIView {

    // 属性
    /// 提示文字
    @IBOutlet weak var tipLabel: UILabel!
    /// 头像图片
    @IBOutlet weak var iconView: UIImageView!
    /// 头像底部约束
    @IBOutlet weak var iconBottomCons: NSLayoutConstraint!
    /// 从xib加载jiemian
    class func welcomeView() -> CLLWelcomeView {
        // 从xib加载界面显示
        let nib = UINib(nibName: "CLLWelcomeView", bundle: nil)
        
        let v:CLLWelcomeView = nib.instantiate(withOwner: nil, options: nil)[0] as! CLLWelcomeView
        
        return v
    }
    
    // 设置动画显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        // 先让xib上的所有控件布局好
        self.layoutIfNeeded()
        
        iconBottomCons.constant = bounds.size.height - 200
        // 设置动画
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
            // 更新约束
            self.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1.0, animations: { 
                self.tipLabel.alpha = 1
            }, completion: { (_) in
                // 移除控件 显示首页
                self.removeFromSuperview()
            })
        }
    }
    
    // 处理UI显示
    override func awakeFromNib() {
        super.awakeFromNib()
        
        guard let urlString = CLLNetWorkManager.shared.userAccount.avatar_large,
              let url = URL(string: urlString)
        else {
            return
        }
      
        iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        // 设置圆形图像
        iconView.layer.cornerRadius = iconView.bounds.width / 2
        iconView.layer.masksToBounds = true
        
    }
}
